package HandshakeJoin;

import java.util.Calendar;
import com.google.common.base.Preconditions;
import org.apache.s4.base.Event;

public class DataEvent extends Event
{
    private int sourceId;
    private long timestamp;
    private int dimensions;
    private int[] fields;

    public DataEvent() {}

    public DataEvent(final int sourceId, final int dimensions,
            final long timestamp) {
        Preconditions.checkArgument(dimensions >= 0);

        this.sourceId = sourceId;
        this.dimensions = dimensions;
        fields = new int[dimensions];
        this.timestamp = timestamp;
    }

    public DataEvent(final int sourceId, final int dimensions) {
        Preconditions.checkArgument(dimensions >= 0);

        this.sourceId = sourceId;
        this.dimensions = dimensions;
        fields = new int[dimensions];
        timestamp = Calendar.getInstance().getTimeInMillis();
    }

    public String getKey() {
        return Integer.toString(fields[0]);
    }

    public int getSourceId() {
        return sourceId;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public int getDimensions() {
        return dimensions;
    }

    public int[] getTuple() {
        return fields;
    }

    public int getField(final int idx) {
        Preconditions.checkElementIndex(idx, dimensions);

        return fields[idx];
    }

    public DataEvent setField(final int idx, final int val) {
        Preconditions.checkElementIndex(idx, dimensions);

        fields[idx] = val;

        return this;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("[Source:" + sourceId + "]");

        for (int i = 0; i < dimensions; ++i) {
            sb.append("  " + fields[i]);
        }
        sb.append("\n");

        return sb.toString();
    }
}